package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.entity.Result;
import com.li.mapper.CheckItemMapper;
import com.li.pojo.CheckItem;
import com.li.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass =CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemMapper checkItemDao;
    /**
     * 新增检查项
     * @param checkItem
     */
    @Override
    public void addCheckItem(CheckItem checkItem) {

        checkItemDao.addCheckItem(checkItem);
    }

    @Override
    public PageResult selectPageByCondition(QueryPageBean queryPageBean) {
        //分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //通过条件进行分页查询
        Page<CheckItem> checkItems = checkItemDao.selectPageByCondition(queryPageBean.getQueryString());
        //获取结果
        PageResult pageResult = new PageResult();

        pageResult.setRows(checkItems.getResult());
        pageResult.setTotal(checkItems.getTotal());
        return pageResult;
    }

    @Override
    public CheckItem findById(Integer id) {
        //通过id查询检查项
        CheckItem byId = checkItemDao.findById(id);


        return byId;
    }

    @Override
    public void deleteById(Integer id) {

        //检查当前项是否被检查组关联
        Long countByCheckItemId = checkItemDao.findCountByCheckItemId(id);

        if(countByCheckItemId>0){
            //被检查组关联 不能删除
           throw new RuntimeException();
        }
        //能够删除
        checkItemDao.deleteById(id);

    }

    @Override
    public List<CheckItem> findAll() {

        return checkItemDao.findAll();

    }

    @Override
    public void edit(CheckItem checkItem) {

        checkItemDao.edit(checkItem);
    }
}
